package com.wx.core.process;

import java.io.IOException;

import javax.xml.bind.JAXBException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wx.core.vo.MsgRequest;
import com.wx.core.vo.MsgResponse;
import com.wx.core.vo.MsgResponseImage;
import com.wx.core.vo.MsgResponseMusic;
import com.wx.core.vo.MsgResponseNews;
import com.wx.core.vo.MsgResponseText;
import com.wx.core.vo.MsgResponseVideo;
import com.wx.core.vo.MsgResponseVoice;
import com.zyht.common.xml.XmlConvertTools;

public class MsgXmlUtil {

	private static final Logger logger = LoggerFactory.getLogger(MsgXmlUtil.class);
	
	private static final XmlConvertTools xmlConvert = new XmlConvertTools();
	
	/**
	 * <b>作者:</b> Bob<br/>
	 * <b>修改时间：</b>2016年10月31日 - 下午2:59:48<br/>
	 * <b>功能说明：</b> 将xml转换成对象<br/>
	 * 
	 * @param xml
	 * @return
	 * @throws JAXBException 
	 * @throws IOException 
	 * @throws Exception
	 */
	public static MsgRequest parseXmlFromString(String xml) throws Exception{
		logger.debug("微信服务器响应报文是：\n" + xml);
		return xmlConvert.convertXml2Bean(MsgRequest.class, xml);
	}
	
	public static final String toXml(MsgResponse response){
		
		String xml = null;
		try{
			xml = xmlConvert.convertObj2Xml(response);
			if(logger.isDebugEnabled()){
				logger.debug("微信服务器响应报文是：\n" + xml);
			}
		} catch (JAXBException e){
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		}
		return xml;
	}
	
	public static String textToXml(MsgResponseText text){

		return toXml(text);
	}

	public static String imageToXml(MsgResponseImage image){

		return toXml(image);
	}

	public static String voiceToXml(MsgResponseVoice voice){

		return toXml(voice);
	}

	public static String videoToXml(MsgResponseVideo video){

		return toXml(video);
	}

	public static String musicToXml(MsgResponseMusic music){

		return toXml(music);
	}

	public static String newsToXml(MsgResponseNews news){

		return toXml(news);
	}
}
